package iscript.coreFramework.tweens 
{

	import flash.utils.getTimer;
	import iscript.coreFramework.tweens.easing.Linear;
 

	public class TweenObjectCore 
	{
		
		public function TweenObjectCore(duration:Number=0, vars:Object=null) 
		{
			   _duration =1000;
		}
		
		private  var  _startPosition:Number=0;		
		private  var  _finishPosition:Number=1000;
		private  var  _currentPosition:Number = 0;
		private  var  _duration:Number;
		protected var _ease:Function =  Linear.easeNone;		
		private var _position:Number;
		
		/*这是as代码，四个参数分别是：
		t：current time（当前时间）；
		b：beginning value（初始值）；
		c： change in value（变化量）；
		d：duration（持续时间）。
		*/
		
		public var onUpdate:Function;		
		public var onComplete:Function;
		
	 
		private var  _startTime:Number;
		private var  _currentTime:Number;
		
		public function start():void {
			_startTime = getTimer();
			_currentTime = _startTime;			
			_isPause = false;
			_isPlaying = false;
			_isFinish = false;
		}
		
		public  function update():void {
			    if (_isPause) return;
				if (_currentTime-_startTime < _duration) {
					_position =_ease(_currentTime-_startTime, _startPosition, _finishPosition-_startPosition, _duration);				
					_currentTime = getTimer(); 
					_isPlaying = true;			
					update();
					if(onUpdate!=null)onUpdate();
				}else {
					_position = _finishPosition;
					_isPlaying = false;
					_isFinish = true;
					_isPause = true;
					if(onUpdate!=null)onUpdate();
					if(onComplete!=null)onComplete();
				}
			}
		
		private var _isPause:Boolean; 
		public function pause():void {
			  _isPause = !_isPause;
			}
			
		private var _isPlaying:Boolean; 
		public function get IsPlaying():Boolean {
				 return _isPlaying = true;
				}	
			
		private var _isFinish:Boolean; 
		public function get IsFinish():Boolean {
				 return _isFinish = true;
				}
				
		public  function get position():Number {
			return _position;
	 	    }

		
	}

}